import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';
import '../widget/widget_wrapper.dart';
import 'string_size.dart';

enum TextFontFamily {
  harmonyOS('HarmonyOS');

  final String family;

  const TextFontFamily(this.family);
}

enum TextFontWeight {
  regular,
  medium,
  bold;
  // semibold,
  // extrabold,
  // heavy;

  FontWeight get toFontWeight => switch (this) {
        TextFontWeight.medium => FontWeight.w500,
        // TextFontWeight.semibold => FontWeight.w600,
        TextFontWeight.bold => FontWeight.w700,
        // TextFontWeight.extrabold => FontWeight.w800,
        // TextFontWeight.heavy => FontWeight.w900,
        _ => FontWeight.normal,
      };
}

extension TextString on String {
  Widget text({
    required Color? color,
    required double? fontSize,
    TextFontWeight weight = TextFontWeight.regular,
    TextFontFamily family = TextFontFamily.harmonyOS,
    TextAlign textAlign = TextAlign.start,
    FontStyle? fontStyle = FontStyle.normal,
    int? maxLines,
    double? maxWidth,
    TextOverflow? overflow,
  }) =>
      Text(
        this,
        style: TextStyle(
          color: color,
          fontStyle: fontStyle,
          fontSize: fontSize,
          fontWeight: weight.toFontWeight,
          fontFamily: family.family,
          // 在非Material树中
          // Text底部会有黄色双横线
          // 这里手动设置为空
          decoration: TextDecoration.none,
        ),
        textAlign: textAlign,
        // 否则仅显示一行
        overflow: switch ((overflow, maxLines)) {
          (!= null, _) => overflow,
          (_, != null) => TextOverflow.ellipsis,
          _ => null,
        },
        maxLines: maxLines,
      )
          // 多行时似乎不起作用
          // .sized(width: maxWidth);
          .constrained(BoxConstraints(
        maxWidth: maxWidth ?? double.infinity,
      ));

  TextSpan textSpan({
    required Color? color,
    required double? fontSize,
    TextFontWeight weight = TextFontWeight.regular,
    TextFontFamily family = TextFontFamily.harmonyOS,
  }) =>
      TextSpan(
        text: this,
        style: TextStyle(
          color: color,
          fontSize: fontSize,
          fontWeight: weight.toFontWeight,
          fontFamily: family.family,
          // 在非Material树中
          // Text底部会有黄色双横线
          // 这里手动设置为空
          decoration: TextDecoration.none,
        ),
      );

  /// 给Text添加边框
  Widget textWithBorderWithFixedHeight({
    required Color? color,
    required double? fontSize,
    TextFontWeight weight = TextFontWeight.regular,
    required Color borderColor,
    required double borderWidth,
    required double height,
    required double hPadding,
    Color? bgColor,
    AlignmentGeometry alignment = AlignmentDirectional.center,
    double borderRadius = 0,
  }) {
    final text = Text(this,
        style: TextStyle(
          color: color,
          fontSize: fontSize,
          fontWeight: weight.toFontWeight,
          fontFamily: TextFontFamily.harmonyOS.family,
          // 在非Material树中
          // Text底部会有黄色双横线
          // 这里手动设置为空
          decoration: TextDecoration.none,
        ));
    return Container(
      alignment: alignment,
      height: height,
      width: text.data!.getTextWidth(text.style!) + hPadding * 2,
      decoration: BoxDecoration(
        color: bgColor, // Added background color
        border: Border.all(color: borderColor, width: borderWidth),
        borderRadius: BorderRadius.circular(borderRadius),
      ),
      child: text,
    );
  }

  /// 删除样式的文字
  Widget deletedText({
    required Color? color,
    required double? fontSize,
    TextFontFamily? family,
    TextFontWeight weight = TextFontWeight.regular,
    int? maxLines,
    double? maxWidth,
  }) =>
      Text(
        this,
        style: TextStyle(
          color: color,
          fontSize: fontSize,
          fontWeight: weight.toFontWeight,
          fontFamily: family?.family,
          decoration: TextDecoration.lineThrough,
          decorationColor: color,
          decorationThickness: 2,
        ),
        // 否则仅显示一行
        overflow: maxLines != null ? TextOverflow.ellipsis : null,
        maxLines: maxLines,
      )
          // 多行时似乎不起作用
          .sized(width: maxWidth);

  /// 下划线样式的文字
  Widget underlinedText({
    required Color? color,
    required double? fontSize,
    TextFontWeight weight = TextFontWeight.regular,
    TextFontFamily family = TextFontFamily.harmonyOS,
    int? maxLines,
    double? maxWidth,
  }) =>
      Text(
        this,
        style: TextStyle(
          color: color,
          fontSize: fontSize,
          fontWeight: weight.toFontWeight,
          fontFamily: family.family,
          decoration: TextDecoration.underline,
          decorationColor: color,
          decorationThickness: 2,
        ),
        // 否则仅显示一行
        overflow: maxLines != null ? TextOverflow.ellipsis : null,
        maxLines: maxLines,
      )
          // 多行时似乎不起作用
          .sized(width: maxWidth);

  /// 下划线样式的文字
  TextSpan underlinedTextSpan({
    required Color? color,
    required double? fontSize,
    TextFontWeight weight = TextFontWeight.regular,
    TextFontFamily family = TextFontFamily.harmonyOS,
    VoidCallback? onTap,
  }) =>
      TextSpan(
        text: this,
        style: TextStyle(
          color: color,
          fontSize: fontSize,
          fontWeight: weight.toFontWeight,
          fontFamily: family.family,
          decoration: TextDecoration.underline,
          decorationColor: color,
          decorationThickness: 2,
        ),
        recognizer: TapGestureRecognizer()..onTap = onTap,
      );

  // Widget gradientText({
  //   required List<Color> colors,
  //   required double? fontSize,
  //   TextFontWeight weight = TextFontWeight.regular,
  //   TextAlign textAlign = TextAlign.start,
  //   TextFontFamily family = TextFontFamily.harmonyOS,
  //   int? maxLines,
  //   double? maxWidth,
  // }) =>
  //     GradientText(
  //       this,
  //       colors: colors,
  //       style: TextStyle(
  //         color: color,
  //         fontSize: fontSize,
  //         fontWeight: weight.toFontWeight,
  //         fontFamily: family.family,
  //         // 在非Material树中
  //         // Text底部会有黄色双横线
  //         // 这里手动设置为空
  //         decoration: TextDecoration.none,
  //       ),
  //       textAlign: textAlign,
  //       // 否则仅显示一行
  //       overflow: maxLines != null ? TextOverflow.ellipsis : null,
  //       maxLines: maxLines,
  //     )
  //         // 多行时似乎不起作用
  //         .sized(width: maxWidth);
}
